package com.tree.rpc.socket.service;

import com.tree.rpc.socket.remote.pojo.RpcTransformObj;

import java.lang.reflect.Method;

/**
 * @ClassName com.tree.rpc.socket.service
 * Description: <类功能描述>. <br>
 * <p>
 * 获取网络成对象数据，调用其数据对应的类的方法
 * </p>
 * @Author tree
 * @Date 2019-11-07 00:14
 * @Version 1.0
 */
public class ServiceDispatcher {


    public static Object dispatch(Object o){
        RpcTransformObj rpcTransformObj = (RpcTransformObj)o;
        String fullClassName = rpcTransformObj.getFullName();
        String methodName = rpcTransformObj.getMethodName();
        Object[] params =rpcTransformObj.getParams();
        Class[] types = new Class[params.length];
        for (int i = 0; i < params.length; i++) {
            types[i] = params[i].getClass();
        }

        Object respObj = null;
        try {
            Class clazz = Class.forName(fullClassName);
            Method method = clazz.getDeclaredMethod(methodName,types);
            respObj = method.invoke(clazz.newInstance(),params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return respObj;

    }
}
